//
//  ViewController.m
//  UI_通讯_26
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 liwenhan. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "FormCell.h"
#import "DetailViewController.h"
#import "AddViewController.h"
#import "NewView.h"
#import "Persen.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)CoreDataManager *manager;

@property (nonatomic, strong)NSArray *picArray;
@property (nonatomic, strong)NSArray *nameArray;

@property (nonatomic, strong)NSArray *allArr;

@property (nonatomic, strong)NewView *newview;

@property (nonatomic, strong)UISearchBar *search;



@end

@implementation ViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(click:)];
    self.title = @"所有联系人";
    
    self.allArr = [NSArray array];
    
    
    self.picArray = [NSArray arrayWithObjects:@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"10.jpg", @"11.jpg", @"12.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"10.jpg", @"11.jpg", @"12.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"10.jpg", @"11.jpg", @"12.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"10.jpg", @"11.jpg", @"12.jpg", nil];
    
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 40)];
    [self.search setPlaceholder:@"Search"];
    [self.view addSubview:self.search];
    // 键盘样式
    [self.search setKeyboardType:UIKeyboardTypeEmailAddress];
    // 是否提供自动自动修正功能
    [self.search setAutocorrectionType:UITextAutocorrectionTypeDefault];
    // 是否显示取消按钮
    [self.search setShowsCancelButton:YES animated:YES];


    // 添加手势
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.view addSubview:v];
    // !把他放在最下面
    [self.view sendSubviewToBack:v];
    v.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    
    // 手势加到视图上
    [v addGestureRecognizer:tap];
    // 点击一次跳转
    tap.numberOfTapsRequired = 1;

    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64 + 40, WIDTH, HEIGHT - 64 -40) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    
    //!!!!!创建单例, 一定要写
    self.manager = [CoreDataManager defaults];
    
    // 获取查询数据的请求, 相当于SQL的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Persen"];
    NSError *error;
    
    self.allArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];

    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 查询数据库数据
    
    // 获取查询数据的请求, 相当于SQL的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Persen"];
    NSError *error;
    
    self.allArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%@", self.allArr);
    
    [self.tableView reloadData];

}


// 点击空白处回收键盘
- (void)tapAction:(UIGestureRecognizer *)tap
{
    [self.search resignFirstResponder];
    self.navigationController.navigationBarHidden = NO;
    self.search.frame = CGRectMake(0, 64, WIDTH, 40);
}



- (void)click:(UIBarButtonItem *)item
{
    AddViewController *addVC = [[AddViewController alloc]init];
   
    // 这么跳转没有controller
    [self.navigationController presentViewController:addVC animated:YES completion:^{
        
    }];
 
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    detailVC.chuanPerson = self.allArr[indexPath.row];
    detailVC.i = indexPath.row;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    FormCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[FormCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        
    }
    
    
    cell.nameLabel.text = [self.allArr[indexPath.row] name];
    cell.phoneLabel.text = [self.allArr[indexPath.row] phone];
    cell.headView.image = [UIImage imageNamed:self.picArray[indexPath.row]];
    
    
    
    return cell;
}

//  左划删除
- (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        Persen *per = self.allArr[indexPath.row];
        
        [self.manager.managedObjectContext deleteObject:per];
        
        [self.manager saveContext];
        
        // 获取查询数据的请求, 相当于SQL的查询语句
        NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Persen"];
        NSError *error;
        
        self.allArr = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
         [self.tableView reloadData];
    }
    
   

   
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
